package handler

import (
	"github.com/gin-gonic/gin"
	"golang-websocket-chatroom/api/request"
	"golang-websocket-chatroom/internal/dao"
	"golang-websocket-chatroom/internal/model"
	"net/http"
)

func Room(c *gin.Context) {
	var req request.Room
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	room := model.Room{}
	dao.GetOneById(req.Id, &room)
	if room.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "房间号不存在",
			"data": nil,
		})
		return
	}
	if room.Name == req.Name {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "进入房间成功",
			"data": room,
		})
	}
}

func RoomList(c *gin.Context) {
	list := dao.ListRoom()
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "消息展示成功",
		"data": list,
	})
}

func RoomAdd(c *gin.Context) {
	var req request.AddChatReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	chat := model.Chat{
		People: req.People,
		Test:   req.Test,
		Img:    req.Img,
	}
	dao.Create(&chat)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "数据传递成功",
		"data": chat,
	})
}
